package org.ruoyi.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 讯飞配置检查控制器
 */
@Slf4j
@RestController
@RequestMapping("/xunfei/config")
public class XunfeiConfigController {

    @Value("${xunfei.spark.app-id:}")
    private String appId;

    @Value("${xunfei.spark.api-key:}")
    private String apiKey;

    @Value("${xunfei.spark.api-secret:}")
    private String apiSecret;

    /**
     * 检查讯飞配置是否正确加载
     */
    @GetMapping("/check")
    public String checkConfig() {
        log.info("Xunfei Config - AppId: {}, ApiKey: {}, ApiSecret: {}", 
                 appId, apiKey != null ? "******" : "null", apiSecret != null ? "******" : "null");
        
        StringBuilder result = new StringBuilder();
        result.append("讯飞配置检查结果:\n");
        result.append("AppId: ").append(appId != null && !appId.isEmpty() ? "已配置" : "未配置").append("\n");
        result.append("ApiKey: ").append(apiKey != null && !apiKey.isEmpty() ? "已配置" : "未配置").append("\n");
        result.append("ApiSecret: ").append(apiSecret != null && !apiSecret.isEmpty() ? "已配置" : "未配置").append("\n");
        
        if (appId == null || appId.isEmpty() || 
            apiKey == null || apiKey.isEmpty() || 
            apiSecret == null || apiSecret.isEmpty()) {
            result.append("错误: 配置不完整，请检查 application.yml 或 application.properties 文件中的配置\n");
        } else {
            result.append("成功: 所有配置均已正确加载\n");
        }
        
        return result.toString();
    }
    
    /**
     * 获取配置详情（仅用于调试，生产环境中应禁用）
     */
    @GetMapping("/detail")
    public String getConfigDetail() {
        StringBuilder result = new StringBuilder();
        result.append("讯飞配置详情:\n");
        result.append("AppId: ").append(appId).append("\n");
        result.append("ApiKey: ").append(apiKey).append("\n");
        result.append("ApiSecret: ").append(apiSecret).append("\n");
        return result.toString();
    }
}